/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

function date(){

	this.showProperties = function(){
		this.tabId = this.propertyBox.addTab("Date");
		this.propertyBox.addProperty(this.tabId, 0, 0, 'name', 'Textbox', this, {
			label: 'Name',
			help: 'Sets the name of the object.'
		});
		this.propertyBox.addProperty(this.tabId, 1, 0, 'label', 'Textbox', this, {
			label: 'Label',
			help: 'Sets the content of the label.'
		});
	}

	this.setProperty = function(property, value){
		// Changes the property of  this object setting it to value.
		switch (property) {
			case 'name':
				this.hidden.name = value;
				break;
			case 'label':
				this.label.innerHTML = value;
				break;
			default:
				break;
		}
	}
	
	this.getProperty = function(property){
		// Gets the property of  this object
		switch (property) {
			case 'name':
				return this.hidden.name;
				break;
			case 'label':
				return this.label.innerHTML;
				break;
			default:
				break;
		}
	}
	
	this.JSON = function(){
		var EO_JSON = {
			label: this.getProperty('label'),
			name: this.getProperty('name')
		};
		return EO_JSON;
	}
	
	this.HTML = function(){
		var html = "<div style=\"position:absolute; top:" + this.getProperty('top') + "; left:" + this.getProperty('left') + "; \">" +
		"<label>" +
		this.getProperty('label') +
		"</label> " +
		"<select id='day' onchange=\"" +
		"document.getElementById('date').value = document.getElementById('day').value + '/' + document.getElementById('month').value + '/' + document.getElementById('year').value;" +
		"\">"
		
		for (var i = 1; i <= 31; i++) {
			html += "<option value=\"" + i + "\">" + i + "</option>";
		}
		
		html += "</select>" +
		"<label >/</label>" +
		"<select id='month' onchange=\"" +
		"document.getElementById('date').value = document.getElementById('day').value + '/' + document.getElementById('month').value + '/' + document.getElementById('year').value;" +
		"\">"
		
		for (var i = 1; i <= 12; i++) {
			html += "<option value=\"" + i + "\">" + i + "</option>";
		}
		
		html += "</select>" +
		"<label>/</label>" +
		"<select id='year' onchange=\"" +
		"document.getElementById('date').value = document.getElementById('day').value + '/' + document.getElementById('month').value + '/' + document.getElementById('year').value;" +
		"\">"
		
		for (var i = 1990; i <= 2011; i++) {
			html += "<option value=\"" + i + "\">" + i + "</option>";
		}
		
		html += "</select>" +
		"<input id=\"date\" type=\"hidden\" name=\"" + this.getProperty('name') + "\"></input>"+
		"</div>"
		
		return html;
	}
	
	this.help = "A field to select a date.";
	
	this.objectList = new Array();
	this.selectorList = new Array();
	
	this.onAppend = function(){
		this.selectorDiv.zIndex = 2;
		this.selectorDiv.style.position = 'absolute';
		var divEO = document.getElementById(this.id);
		this.selectorDiv.style.top = '0px';
		this.selectorDiv.style.left = '0px';
		this.selectorDiv.style.width = divEO.offsetWidth + 'px';
		this.selectorDiv.style.height = divEO.offsetHeight + 'px';
	}
	
	this.init = function(){
	
		this.objectList.push('div');
		this.objectList.push('selectorDiv');
		this.selectorList.push('selectorDiv');
		
		var div = document.createElement('div');
		this.div = div;
		
		var label = document.createElement('label');
		label.innerHTML = 'Date: ';
		this.label = label;
		
		var bar1 = document.createElement('label');
		bar1.innerHTML = '/';
		this.bar1 = bar1;
		
		var bar2 = document.createElement('label');
		bar2.innerHTML = '/';
		this.bar2 = bar2;
		
		var option = null;
		var daySelector = document.createElement('select');
		for (var i = 1; i <= 31; i++) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = i;
			daySelector.appendChild(option);
		}
		this.day = daySelector;
		
		var monthSelector = document.createElement('select');
		for (var i = 1; i <= 12; i++) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = i;
			monthSelector.appendChild(option);
		}
		this.month = monthSelector;
		
		var yearSelector = document.createElement('select');
		for (var i = 1900; i <= 2011; i++) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = i;
			yearSelector.appendChild(option);
		}
		this.year = yearSelector;
		
		var hidden = document.createElement('input');
		hidden.type = 'hidden';
		hidden.value = "1/1/1900";
		this.hidden = hidden;
		
		div.appendChild(label);
		div.appendChild(daySelector);
		div.appendChild(bar1);
		div.appendChild(monthSelector);
		div.appendChild(bar2);
		div.appendChild(yearSelector);
		
		var selectorDiv = document.createElement('div');
		this.selectorDiv = selectorDiv;	
	}
}	